package readFile;

import org.springframework.util.StopWatch;

import java.io.*;
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import java.lang.management.MemoryUsage;

public class ReadFile02 {

    public static void main(String[] args) {
        StopWatch stopWatch  = new StopWatch();
        stopWatch.start();
        int count = 0;
        try(BufferedReader fileRead = new BufferedReader(new FileReader(new File("D:\\code.txt")))) {
            String line;
            while ((line = fileRead.readLine()) != null){
                //System.out.println(line);
                count ++;
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        stopWatch.stop();
        System.out.println("read all lines spend " + stopWatch.getTotalTimeSeconds()+ " s");
        MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
        MemoryUsage memoryUsage = memoryMXBean.getHeapMemoryUsage();
        long initMemory = memoryUsage.getInit();
        long usedMemory = memoryUsage.getUsed();
        System.out.println("read line count :" + count);
        System.out.println("init memory :" + initMemory/ (1024 * 1024) + "M");
        System.out.println("used memory :" + usedMemory/ (1024 * 1024) + "M");
    }
}
